<template>
  <div class="coupon">
    <div class="top">
      <span @click="back">返回</span>
      <span>我的优惠</span>
    </div>
    <div v-if="userLoginMsg" class="envelope">
      <van-tabs v-model="active">
        <van-tab title="我的红包">
          <div class="item" v-for="(v, i) in hongbao" :key="i">
            <section class="left">
              <p v-text="'￥' + v.amount.toFixed(1)"></p>
              <p v-text="'满' + v.sum_condition + '元可用'"></p>
            </section>
            <section class="content">
              <p v-text="v.name"></p>
              <p v-text="v.end_date + '到期'"></p>
              <p v-text="v.description_map.phone"></p>
            </section>
            <section class="right">
              <p v-text="'剩余'+sday(v.begin_date, v.end_date)+'天到期'"></p>
            </section>
            <section v-if="v.limit_map" class="bottom">
              <p v-text="v.limit_map.restaurant_flavor_ids"></p>
            </section>
          </div>
          <!-- <div class="hisHongbao">查看历史红包</div> -->
        </van-tab>
        <van-tab title="商家优惠券">
          <el-empty>
            <el-button type="primary">下载或升级客户端</el-button>
          </el-empty>
        </van-tab>
      </van-tabs>
          <footer>
      <el-button @click="exchange">兑换红包</el-button>
      <el-button>推荐有奖</el-button>
    </footer>
    </div>
    <div v-else class="notLogin">
        <el-empty>
  <el-button type="primary" @click="jumpToLogin">去登录</el-button>
</el-empty>
    </div>

  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "coupon",
  data() {
    return {
      active: 0,
      hongbao: "",
    };
  },
  created() {
    this.getUseHongBao();
  },
  methods: {
    back() {
      this.$router.back();
    },
    jumpToLogin(){
        this.$router.push({
            name:'login'
        })
    },
    // 获取可用红包
    getUseHongBao() {
      let api =
        this.eleUrl.hongbaos +
        this.userLoginMsg.id +
        "/hongbaos?limit=20&offset=0";
      this.$http({
        url: api,
        methods: "get",
      }).then((res) => {
        this.hongbao = res.data;
        console.log(res.data);
      });
    },
    // 剩余时间
    sday: (start, end) => {
      var dateBegin = new Date(start);
      var dateEnd = new Date(end);
      let stime = parseInt(dateBegin.getDay());
      let etime = parseInt(dateEnd.getDay());
      let residualTime = etime - stime;
      return residualTime;
    },
    // 兑换红包
    exchange(){
      this.$router.push({
        name:'exchange'
      })
    }
  },
  computed: {
    ...mapState(["eleUrl", "userLoginMsg"]),
  },
};
</script>

<style scoped>
.coupon {
  font-size: 0.15rem;
  background-color: rgb(243, 241, 241);
}
.top {
  height: 0.4rem;
  width: 100%;
  background-color: rgb(80, 143, 238);
}
.top > span {
  color: white;
  line-height: 0.4rem;
  margin-left: 0.2rem;
}
.top > span:nth-child(2) {
  margin-left: 1.2rem;
  font-weight: bold;
}
/* 红包信息 */
.item {
    background-color: #fff;
  border-top: 0.01rem solid rgb(238, 178, 178);
  overflow: hidden;
  margin-top: .1rem;
  height: .8rem;
  padding-top: .3rem;
}
.left,
.content,
.right {
  float: left;
  overflow: hidden;
}
.left{
    width: 20%;
}
.left>p:nth-child(1){
    line-height: .3rem;
    color: red;
    font-size: .2rem;
    text-align: center;
}
.left>p:nth-child(2){
     color: gray;
    font-size: .1rem;
    text-align: center;
}
.content{
    color: gray;
    font-size: .1rem;
    line-height: .2rem;
}
.content>p:nth-child(1){
    font-size: .18rem;
}
.right{
    float: right;
    color: red;
}
.bottom{
    float: left;
    font-size: .1rem;
    background-color: rgb(236, 234, 234);
    padding: .1rem;
    width: 100%;
    color: gray;
}
/* 查看历史红包 */
/* .hisHongbao{
    margin-top: .2rem;
    text-align: center;
    font-size: .1rem;
} */
/* 未登录状态 */
.notLogin{
    padding: .3rem;
    text-align: center;
}
/* 底部 */
footer{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
footer .el-button{
  float: left;
  width: 50%;
  margin: 0;
}
</style>